<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="com.sinodata.bsm.center.purview.FunctionConstants"%>
<%@ page import="com.sinodata.bsm.center.purview.UserFunctionValidate"%>
<%@ include file="../include/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
	<title>采集任务</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="pragma" content="no-cache"/>
	<meta http-equiv="cache-control" content="no-cache"/>
	<meta http-equiv="expires" content="0"/>  
	<%@ include file="../include/common.jsp"%>
	<%@ include file="../include/table.jsp"%>
	<%@ include file="../include/jbox.jsp"%>
	<script type="text/javascript">
		$(document).ready(function(){
			var oTable =$("#tasks").dataTable({
				"bProcessing": true, 
				"bServerSide": true,
				"sServerMethod": 'POST',
				"sAjaxSource":"collect-task!list.action?resId=${resId}",
				"bPaginate":false,
				"bSort": true,
				"bFilter":false,
				"sPaginationType": 'full_numbers',
				"aoColumns":[
				<%if(UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER)){%>
					{"mDataProp":"operate","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
						var statuValue = oData.statusIndex==1?'禁用':'启用';
						var statuImg = oData.statusIndex==1?"../style/default/bsm/images/stop.gif":"../style/default/bsm/images/start.gif";
						var html ="<div style='text-align:left; margin-left:5px; width:80px; height:20px;'><a href='#"+oData.id+"' onclick='updateState("+oData.id+")' ><img width='16px' height='16px' src='"+statuImg+"' title='"+statuValue+"'></a>&nbsp;&nbsp;";
							html +="<a href='#' onclick='update("+oData.id+")'><img src='../style/default/bsm/images/edit.gif' title='修改'></a>&nbsp;&nbsp;";
							if(oData.resId!=null&&oData.resId!=''){
								html +="<a href='#"+oData.id+"' onClick='del("+oData.id+")'><img src='../style/default/bsm/images/delete.gif' title='删除' /></a></div>";
							}else{
								html+="</div>";
							}
						$(nTd).html(html).css("padding","0");
					}},
					<%}%>
					{"mDataProp":"collectorName","bSortable": false},
					{"mDataProp":"property","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
						var propertys=sData.split(',');
						var flag = false;
						if(propertys.length>=1&&sData!=''){
							$(nTd).html(""+propertys[0]+"(<label style='color:green;' >"+(propertys.length)+"</label>)");
						}
					}},
					{"mDataProp":"schedule","bSortable": false},
					{"mDataProp":"isSave","bSortable": false},
					{"mDataProp":"status","bSortable": false,"fnCreatedCell":function(nTd, sData, oData, iRow, iCol){
						var imgSrc = oData.statusIndex==1?"collect_Enable.png":"collect_disable.png";
						$(nTd).html("<img width='16px' height='16px'src='../style/default/bsm/images/"+imgSrc+"' title='"+sData+"' />");
					}},
					{"mDataProp":"isDefault","bSortable": false},
					{"mDataProp":"statusIndex","bVisible":false},
					{"mDataProp":"id","bVisible":false},
					{"mDataProp":"resId","bVisible":false}
				],
				"oLanguage":{
					"sZeroRecords": "没有找到记录",
					"sInfo":"",
					"sInfoEmpty": ""
				}
			});
			
			$('#tasks tbody tr').live('click', function (e) {
					//显示资源明细
	    			if ( oTable.fnIsOpen(this) ) {
	    				oTable.fnClose( this ); 
	    			}else{
	    			    //if($(e.target).parent('tr').attr('_DT_RowIndex')!=null&&$(e.target).parent('tr').attr('_DT_RowIndex')!=''){
	    			    	 oTable.fnOpen(this,fnFormatDetails(oTable,this),'details');
	    			    //}
	    			}
			});
		});
		
		function fnFormatDetails ( oTable, nTr ) {  
    		var propertys = oTable.fnGetData( nTr ).property.split(','); 
    		if(propertys.length>0){
    			var sOut = '<table cellpadding="5" name="propertyTbl" cellspacing="0" border="0" style="padding-left:50px;"><tr><td>采集指标：'; 
				$.each(propertys,function(index,ob){
					sOut += ''+ob;
					if(index!=(propertys.length-1)){
						sOut +='，';
					}     
				});
    			 sOut += '</td></tr></table>';   
    			 return sOut; 
    		}
       } 
       
       function update(id){
       	if(isDel(id)==true){
				getTopWin(window).getMessageBox().alert('采集任务已被删除',"");
				var oTable =$("#tasks").dataTable();
				oTable.fnClearTable();
				return;
		}
       	
       	if(id=='310001'){
           	location.assign('collect-task!initUpdate.action?id='+id+'&resId=${resId}');
       	}else if (id='310002'){
       		
       	}else if (id='310003'){
       		
       	}else{
           	location.assign('collect-task!initUpdate.action?id='+id+'&resId=${resId}');
       	}
       }
		
		function del(id){
			var submit = function(v, h, f){
			  	if (v == 'ok'){
			  		if(isDel(id)==true){
						getTopWin(window).getMessageBox().alert('采集任务已被删除',"");
						var oTable =$("#tasks").dataTable();
						oTable.fnClearTable();
						return;
					}
			  	 	getTopWin(window).getMessageBox().tip("删除中...", 'loading');
			  	 	$.ajax({
						type:'post',
						url:'collect-task!delete.action?id='+id,
						global:false,
						async:false,
						cache:false,
						dataType:'json',
						success:function(data,textStatus){
							window.setTimeout(function () {
								getTopWin(window).getMessageBox().tip('删除成功。', 'success'); 
				 				var obTable = $("#tasks").dataTable();
								obTable.fnClearTable(); 
							},1500);
						},
						error:function(){
							getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
						}
					});
			  	}
		  	}
			
			getTopWin(window).getMessageBox().confirm('确定删除？删除后，恢复默认设置。','提示',submit);
		}
		
		function updateState(taskId){
		
			var submit = function(v, h, f){
			  	if (v == 'ok'){
			  		if(isDel(taskId)==true){
						getTopWin(window).getMessageBox().alert('采集任务已被删除',"");
						var oTable =$("#tasks").dataTable();
						oTable.fnClearTable();
						return;
					}
					$.ajax({
							type:'post',
							url:'collect-task!updateState.action',
							data:{id:taskId,resId:'${resId}'},
							global:false,
							async:false,
							cache:false,
							dataType:'json',
							success:function(data,textStatus){
								getTopWin(window).getMessageBox().tip('修改成功。', 'success'); 
								var obTable = $("#tasks").dataTable();
								obTable.fnClearTable(); 
							},
							error:function(){
								getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
							}
					});
				}
			}
			getTopWin(window).getMessageBox().confirm('确定操作?','提示',submit);
		}
		
		function applyWindow(){
			var oTable = $("#tasks").dataTable();
			var nNodes = oTable.fnGetNodes(); 
			if(nNodes.length>=1){
				var url ='iframe:resource/res-query!apply.action?resId=${resId}';
			    var opt =  {title:'复制到其他资源',id:'applyResWindow',dragLimit:false,width:700,height:445,iframeScrolling:'no',submit:function(v, h, f){},buttons:{}};
			    getTopWin(window).openjBox(url,opt,'taskCommonWindow',window);
			}else{
				getTopWin(window).getMessageBox().alert('无可复制的采集任务','提示');
			}
		}
		
		//应用资源
		applyRes = function (resIds){
			getTopWin(window).getMessageBox().tip("执行中...", 'loading');
				$.ajax({
						type:'post',
						url:'collect-task!batchSave.action?ids='+resIds+'&resId=${resId}',
						global:false,
						async:false,
						cache:false,
						dataType:'json',
						success:function(data,textStatus){
							window.setTimeout(function () {
								getTopWin(window).getMessageBox().tip('应用资源成功。', 'success'); 
							},1500);
						},
						error:function(){
							getTopWin(window).getMessageBox().alert('服务器内部错误,请联系管理员','错误');
						}
				});
		}
		
		function openSelectCollect(){
			$.jBox.open("html:"+$('#collectDiv').clone(true).html()+"",'选择采集器',300,'auto',{buttons:''});	
		}
		
		var collectId = '';
		function changeCollectdId(id){
			collectId = id;
		}
		function toCollectTaskAdd(){
			if(collectId==''){
				collectId = $('#collect').val();
			}
			$.jBox.close();
			if(collectId==310001){
	           	location.assign('collect-task!inputLog.action?id='+collectId+'&resId=${resId}');
	       	}else if (collectId==310002){
	           	location.assign('collect-task!inputLog.action?id='+collectId+'&resId=${resId}');
	       	}else if (collectId==310003){
	           	location.assign('collect-task!inputLog.action?id='+collectId+'&resId=${resId}');
	       	}else{
			    location.assign('collect-task!input.action?id='+collectId+'&resId=${resId}');
	       	}
		}
		
		//验证是否删除
		function isDel(id){
			var isFlag = false;
			$.ajax({
					type:'post',
					url:'collect-task!isDelete.action',
					data:{id:id},
					global:false,
					async:false,
					cache:false,
					dataType:'json',
					success:function(data,textStatus){
						isFlag = data;
					},
					error:function(){
						getTopWin(window).getMessageBox().alert('检验采集任务是否删除出错!','错误');
					}
			});
			return isFlag;
		}
	</script>
	
  </head>
  <body>
  	<h2 class="title-h2"><span><img src="${pageContext.request.contextPath}/style/default/bsm/images/arrow-down.gif"/></span>采集任务列表</h2>
  	<div id="taskDiv">
  	<%boolean sysManager = UserFunctionValidate.sysFunctionValidate(FunctionConstants.SYSTEM_MANAGER);%>
	   <%if(sysManager){%> 
  		<div class ="toolbar" id="toolbar" >
  			<a href="#" onclick="openSelectCollect()" class="page_add" >新增</a>
  			<span class="page_s" >&nbsp;</span>
  			<a href="#" onclick="applyWindow()" class="page_edit" >复制到同类资源</a>
  		</div>
  		<%}%>
  		<table width="100%"  id="tasks" cellpadding="0" cellspacing="0" border="0" class="display"  >
			<thead>
    	 		<tr>
    	 		<% if(sysManager){%>
    	 			<th width="60px">操作</th>
    	 		<%}%>
    	 			<th >采集任务名称</th>
    	 			<th>采集指标</th>
    	 			<th>调度方式</th>
    	 			<th width="60px">是否保存</th>
    	 			<th width="30px">状态</th>
    	 			<th width="60px">是否默认</th>
    	 		</tr>
    	    </thead>
    	</table>
  	</div>
  	<div id="collectDiv" style="display: none;">
  	    <div style="padding:10px;" >
	  		<span>请选择采集器：</span>
	  		<span>
	  			<select   name="collect" id="collect" style="width:180px;"  onchange="changeCollectdId(this.value)" >
	  				<c:forEach items="${collectors}" var="c">
	  					<option value="${c.id}">${c.name}</option>
	  				</c:forEach>
	  			</select>
	  		</span>
	  		<div style="height:20px;text-align:right;margin-top:5px;">
	  			<input type="button" value="确定" class="btn-4" onclick="toCollectTaskAdd();" />
	  		</div>
	  	</div>
  	</div>
  </body>
</html>
